package com.dingding.service.usermangerservice.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.bean.databean.request.ReqGetCurrentCity;
import com.dingding.bean.databean.request.ReqGetPushUerInfo;
import com.dingding.bean.databean.request.ReqGetTokenByRongCloud;
import com.dingding.bean.databean.request.ReqGetUserDetailById;
import com.dingding.bean.databean.request.ReqResetAddress;
import com.dingding.bean.databean.request.ReqScanInfo;
import com.dingding.bean.databean.request.ReqUpdateUserInfo;
import com.dingding.bean.databean.request.ReqUploadImage;
import com.dingding.bean.databean.response.DataResponse;
import com.dingding.bean.databean.response.ResGetPushUserInfo;
import com.dingding.bean.databean.response.ResScanInfo;
import com.dingding.bean.databean.response.ResUploadImage;
import com.dingding.bean.databean.response.ResUserBaseInfo;
import com.dingding.bean.entity.Userdetail;
import com.dingding.bean.entity.Userroute;
import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.beans.Userordert;
import com.dingding.common.core.beans.special.Specialuserinfo;
import com.dingding.common.core.config.ServiceCode;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.im.IInstantMessenger;
import com.dingding.common.core.im.InstantMessengerFactory;
import com.dingding.common.core.im.dto.FormatType;
import com.dingding.common.core.util.GetDistanceUtil;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.dto.CalculatDistanceDTO;
import com.dingding.service.BaseService;
import com.dingding.service.usermangerservice.IUserDetailService;
import com.dingding.upload.ImageUpload;

/**
 * 
 * @Title: UserDetailServiceImpl
 * @Description: 用户详情service
 * @author Li Yi
 * @date 2015-10-27 下午05:27:18
 */
@SuppressWarnings("all")
public class UserDetailServiceImpl extends BaseService implements IUserDetailService {

	private static final Logger log = Logger.getLogger(UserDetailServiceImpl.class);

	// 获取IM通信对象
	private IInstantMessenger instantMessenger = InstantMessengerFactory.createInstantMessengerPaltform();

	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;
	
	/**
	 * 
	 * @Title: uploadImage @Description:上传用户头像 @author cheshaowei @throws
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void uploadImage(ReqUploadImage reqUploadImage, DataResponse dataResponse) {

		// 验证是否存在空数据
		boolean isTrue = this.isUploadImageTrue(reqUploadImage);
		if (!isTrue) {
			log.error("上传头像请求数据为空:" + reqUploadImage.toString());
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		// 用户ID
		String userId = reqUploadImage.getUserId();
		// 用户头像
		String userAvatar = reqUploadImage.getUserAvatar();

		// 获取用户信息
		Userdetail userdetail = this.getUserDetail(userId, dataResponse);
		// 获取用户信息失败
		if (userdetail == null) {
			return;
		}

		// 生成图片名称
		String userAvatarName = ToolUtil.creatImageName();
		// 压缩的图片名称
		String userMiniAvatarName = ToolUtil.creatImageName();
		// 执行图片上传
		ImageUpload imageUpload = new ImageUpload();
		boolean isSuccess = imageUpload.userAvatarUpload(userAvatar, userAvatarName, userMiniAvatarName,
				userdetail.getUserAvatar(), userdetail.getUserTabloidAvatar());

		if (!isSuccess) { // 上传失败
			setStatus(dataResponse, SystemCode.UPLOAD_ERROR);
			return;
		}

		// 头像名称
		userdetail.setUserAvatar(userAvatarName);
		// 头像缩略图地址
		userdetail.setUserTabloidAvatar(userMiniAvatarName);
		// 设置头像地址
		ResUploadImage resUploadImage = new ResUploadImage(ToolUtil.imageAddress(userAvatarName),
				ToolUtil.imageAddress(userMiniAvatarName));

		setStatus(dataResponse, SystemCode.SUCCESS);
		dataResponse.setResponseBody(resUploadImage);
	}

	@Override
	public void setStatus(DataResponse dataResponse, int status) {
		String message = null;

		switch (status) {
		case SystemCode.SUCCESS:// 请求成功
			message = SystemCode.SUCCESS_MESSAGE;
			break;
		case SystemCode.RESET_FAIL_ERROR:// 原密码不正确
			message = SystemCode.RESET_FAIL_MESSAGE;
			break;
		case SystemCode.LOGIN_FAIL_ERROR:// 用户名或密码错误
			message = SystemCode.LOGIN_FAIL_MESSAGE;
			break;
		case SystemCode.WITHDRAWALS_FAIL_ERROR:// 提现密码输入不正确
			message = SystemCode.WITHDRAWALS_FAIL_MESSAGE;
			break;
		case SystemCode.DATA_NULL_ERROR:// 客户端请求数据为空
			message = SystemCode.DATA_NULL_MESSAGE;
			break;
		case SystemCode.ERROR:// 系统异常
			message = SystemCode.ERROR_MESSAGE;
			break;
		case SystemCode.USER_INFO_NOT_EXIST_ERROR:// 用户不存在
			message = SystemCode.USER_INFO_NOT_EXIST_MESSAGE;
			break;
		case SystemCode.REGISTER_USER_EXIST_ERROR:// 用户已存在
			message = SystemCode.REGISTER_USER_EXIST_MESSAGE;
			break;
		case SystemCode.NICK_NAME_ILLEGAL_ERROR: // 昵称不合法
			message = SystemCode.NICK_NAME_ILLEGAL_MESSAGE;
			break;
		case SystemCode.DISTANCE_SHORT_ERROR: // 距离过短
			message = SystemCode.DISTANCE_SHORT_ERROR_MESSAGE;
			break;
		}

		dataResponse.setResponseCode(status);
		dataResponse.setResponseMessage(message);
	}

	/**
	 * @Title: getUserdetail @Description: 地址设置 @param reqResetAddress
	 *         设置地址-客户端请求参数 @param userdetail @author Li Yi @return void @throws
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void resetAddress(ReqResetAddress reqResetAddress, DataResponse dataResponse) {

		// 验证地址设置是否为空
		boolean flag = this.validateResetAddress(reqResetAddress);

		if (!flag) {
			log.error("设置地址请求参数为空:" + reqResetAddress.toString());
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);// 客户端请求不存在
			return;
		}

		// 计算添加起点和终点的距离
		CalculatDistanceDTO calculatDistanceDTO = GetDistanceUtil.getSingleDistance(reqResetAddress.getHomeLongitude(),
				reqResetAddress.getHomeLatitude(), reqResetAddress.getCompanyLongitude(),
				reqResetAddress.getCompanyLatitude());

		// 判断起点和终点的距离是否小于一公里
		if (calculatDistanceDTO.getDistance().compareTo(new BigDecimal(1)) < 0) {
			setStatus(dataResponse, SystemCode.DISTANCE_SHORT_ERROR);
			return;
		}

		// 用于接收用户详情信息
		Userdetail userdetail = this.getUserDetail(reqResetAddress.getUserId(), dataResponse);

		// 判断用户详情信息是否为空
		if (userdetail == null) {
			return;
		}

		// 根据请求bean设置用户详情表中的地址信息
		super.userDetailDao.setUserdetailAddress(reqResetAddress, userdetail);

		List<Userroute> list = new ArrayList<Userroute>();

		// 保存地址同时设置用户路线(2条)
		firstAdd(reqResetAddress.getUserId(), userdetail, dataResponse, list);

		setStatus(dataResponse, SystemCode.SUCCESS);// 请求成功
	}

	/**
	 * @Title: isUploadImageNull @Description: 判断上传头像参数是否为空 @param
	 *         reqUploadImage @author lijinchao @return true 参数正确 false
	 *         参数不正确 @throws
	 */
	private boolean isUploadImageTrue(ReqUploadImage reqUploadImage) {

		if (reqUploadImage == null) {
			return false;
		}
		// 用户id
		if (ToolUtil.isBlank(reqUploadImage.getUserId())) {
			return false;
		}
		// 用户头像图片
		if (ToolUtil.isBlank(reqUploadImage.getUserAvatar())) {
			return false;
		}

		return true;
	}

	/**
	 * @Title: validateResetAddress @Description: 验证设置地址的参数是否为空 @param
	 *         reqResetAddress 客户端传入参数是否为空 @return true 参数正确 false 参数错误 @throws
	 */

	public boolean validateResetAddress(ReqResetAddress reqResetAddress) {

		if (reqResetAddress == null // 设置地址时客户端传入参数是否为空
				|| ToolUtil.isBlank(reqResetAddress.getUserId()) // 用户Id是否为空
				|| ToolUtil.isBlank(reqResetAddress.getHomeAddress()) // 家地址是否为空
				|| reqResetAddress.getHomeLongitude() == null // 家经度是否为空
				|| reqResetAddress.getHomeLatitude() == null // 家纬度是否为空
				|| ToolUtil.isBlank(reqResetAddress.getCompanyAddress()) // 公司地址是否为空
				|| reqResetAddress.getCompanyLongitude() == null // 公司经度是否为空
				|| reqResetAddress.getCompanyLatitude() == null // 公司纬度是否为空
				|| reqResetAddress.getStartWorkTime() == null // 上班时间是否为空
				|| reqResetAddress.getEndWorkTime() == null) { // 下班时间是否为空

			return false;
		}

		return true;
	}

	/**
	 * 
	 * @Title: scanUserInfo @Description: 查看对方用户信息 @param reqScanInfo
	 *         查看对方用户信息-客户端请求参数 @param dataResponse @author Li Yi @return
	 *         void @throws
	 */
	@Override
	@Transactional(readOnly = true)
	public void scanUserInfo(ReqScanInfo reqScanInfo, DataResponse dataResponse) {

		// 验证查看对方用户信息时传入参数是否为空
		boolean flag = this.validateScanUserInfo(reqScanInfo);

		if (!flag) {
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);// 客户端请求数据不存在
			return;
		}

		// 查看用户信息响应bean
		ResScanInfo resScanInfo = new ResScanInfo();

		try {
			// 设置获取对方用户信息，并赋值给响应Bean resScanInfo
			super.userDetailDao.getResScanInfo(reqScanInfo, resScanInfo);

		} catch (Exception e) {
			log.error("查看对方信息出现网络异常, 类：" + this.getClass() + " 方法：scanUserInfo()", e);
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		// 判断响应bean是否空
		if (resScanInfo == null || ToolUtil.isBlank(resScanInfo.getNickName())) {
			setStatus(dataResponse, SystemCode.USER_INFO_NOT_EXIST_ERROR);// 用户不存在
			return;
		}

		// 封装相应请求
		dataResponse.setResponseBody(resScanInfo);

		// 设置响应码
		setStatus(dataResponse, SystemCode.SUCCESS);

	}

	/**
	 * @Title: validateScanUserInfo @Description: 查看对方用户信息时传入参数是否为空 @param
	 *         reqScanInfo 客户端请求参数 @return boolean 返回类型 @throws
	 */
	public boolean validateScanUserInfo(ReqScanInfo reqScanInfo) {

		if (reqScanInfo == null // 客户端请求参数是否为空
				|| ToolUtil.isBlank(reqScanInfo.getSideId()) // 对方用户ID是否为空
				|| reqScanInfo.getSideRole() == null) { // 对方用户角色是否为空

			return false;
		}

		return true;
	}

	/**
	 * 更新用户信息
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateUserInfo(ReqUpdateUserInfo reqUpdateUserInfo, DataResponse dataResponse) {

		// 验证请求参数
		boolean flag = this.validateUpdateUserInfo(reqUpdateUserInfo);

		// 如果请求参数为空
		if (!flag) {
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		// 昵称过长 或者 存在特殊符号
		if (ToolUtil.isNotBlank(reqUpdateUserInfo.getNickName()) && (reqUpdateUserInfo.getNickName().length() > 128
				|| !reqUpdateUserInfo.getNickName().matches(ServiceCode.NICK_NAME_CODE))) {

			setStatus(dataResponse, SystemCode.NICK_NAME_ILLEGAL_ERROR);
			return;
		}

		// 获取用户详情
		Userdetail userdetail = this.getUserDetail(reqUpdateUserInfo.getUserId(), dataResponse);
		// 查询有误
		if (userdetail == null) {
			return;
		}

		super.userDetailDao.updateUserInfo(reqUpdateUserInfo, userdetail);

		setStatus(dataResponse, SystemCode.SUCCESS);
		return;
	}

	/**
	 * @Title: validateUpdateUserInfo @Description: 更新用户信息请求数据是否为空 @param
	 *         reqUpdateUserInfo @author lijinchao @return boolean @throws
	 */
	private boolean validateUpdateUserInfo(ReqUpdateUserInfo reqUpdateUserInfo) {

		if (reqUpdateUserInfo == null || ToolUtil.isBlank(reqUpdateUserInfo.getUserId()) // 用户id
				|| (ToolUtil.isBlank(reqUpdateUserInfo.getNickName()) // 用户昵称
						&& reqUpdateUserInfo.getAge() == null // 年龄
						&& ToolUtil.isBlank(reqUpdateUserInfo.getOccupation()) // 职业
						&& ToolUtil.isBlank(reqUpdateUserInfo.getOrigin()) && reqUpdateUserInfo.getSex() == null)) { // 籍贯

			return false;
		}

		return true;
	}

	/**
	 * @Title: getUserDetail @Description: 根据用户id获取用户信息 @param userId
	 *         用户id @param dataResponse 响应数据 @author lijinchao @return
	 *         Userdetail 用户详情 @throws
	 */
	@Override
	@Transactional(readOnly = true)
	public Userdetail getUserDetail(String userId, DataResponse dataResponse) {

		// 创建用户详情对象
		Userdetail userdetail = null;
		// criterion查询，以用户id查询该用户的详细信息
		Criterion criterion = Restrictions.eq("loginId", userId);

		try {

			userdetail = super.userDetailDao.queryCriteriaUnResult(criterion, false);
		} catch (Exception e) {

			log.error("查询用户详情出现异常, 类：" + this.getClass() + " 方法：getUserDetail()", e);
			setStatus(dataResponse, SystemCode.ERROR);
			return null;
		}

		// 不存在该用户的详情
		if (userdetail == null) {
			log.error("不存在该用户的详情, 类：" + this.getClass() + " 方法：getUserDetail()");
			setStatus(dataResponse, SystemCode.USER_INFO_NOT_EXIST_ERROR);
			return null;
		}

		return userdetail;
	}

	/**
	 * @Title: getCurrentCity @Description: 更新用户当前所在城市 @param
	 *         reqGetCurrentCity @param dataResponse @author lijinchao @return
	 *         void @throws
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void getCurrentCity(ReqGetCurrentCity reqGetCurrentCity, DataResponse dataResponse) {

		// 检验参数是否存在
		if (reqGetCurrentCity == null || ToolUtil.isBlank(reqGetCurrentCity.getUserId())
				|| ToolUtil.isBlank(reqGetCurrentCity.getCurrentCity())) {
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		// 获取用户详情
		Userdetail userdetail = this.getUserDetail(reqGetCurrentCity.getUserId(), dataResponse);
		// 查询有误
		if (userdetail == null) {
			return;
		}

		// 更新用户当前城市
		super.userDetailDao.updateCurrentCity(reqGetCurrentCity.getCurrentCity(), userdetail);

		setStatus(dataResponse, SystemCode.SUCCESS);
	}

	/**
	 * @Title: getPushUserInfo @Description: 获取对方用户信息 @param
	 *         reqGetPushUerInfo @param dataResponse @author lijinchao @return
	 *         void @throws
	 */
	@Override
	@Transactional(readOnly = true)
	public void getPushUserInfo(ReqGetPushUerInfo reqGetPushUerInfo, DataResponse dataResponse) {

		// 验证参数是否为空
		if (reqGetPushUerInfo == null || ToolUtil.isBlank(reqGetPushUerInfo.getUserId())
				|| ToolUtil.isBlank(reqGetPushUerInfo.getOid())) {

			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		// 根据订单Id获取订单信息
		Criterion criterion = Restrictions.eq("oid", reqGetPushUerInfo.getOid());
		Userordert userordert = null;

		try {
			userordert = super.userOrderTDao.queryCriteriaUnResult(criterion, false);
		} catch (Exception e) {

			log.error("抢单推送失败后，根据订单id获取订单信息出现异常 类：" + this.getClass() + " 方法： getPushUserInfo()", e);
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		// 判断该订单是否存在
		if (userordert == null) {

			setStatus(dataResponse, SystemCode.ORDER_NOT_EXIST_ERROR);
			return;
		}

		// 获取对方用户信息
		Object obj = userDetailDao.getPushUserInfo(reqGetPushUerInfo, userordert);

		// 判断获取对方用户信息时，返回值
		if (obj instanceof Integer) {
			// 异常
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		setStatus(dataResponse, SystemCode.SUCCESS);
		dataResponse.setResponseBody((ResGetPushUserInfo) obj);
	}

	/**
	 * @Title: getAllUserDetail @Description: 获取全部用户详情(数据迁移专用) @author
	 *         yhq @return void @throws
	 */
	@Override
	@Transactional(readOnly = true)
	public List<Userdetail> getAllUserDetail() {

		return super.userDetailDao.queryAll(false);
	}

	/**
	 * @Title: deleteAllUserDetail @Description: 删除全部用户详情(数据迁移专用) @author
	 *         yhq @return void @throws
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean deleteAllUserDetail() {
		// 全部订单
		int row = 0;
		try {
			row = super.userDetailDao.updateSqlRow("DELETE FROM userdetail");
		} catch (Exception e) {
			log.info("删除全部的用户详情信息时出现异常   类：" + this.getClass() + " deleteAllUserDetail()", e);
			return false;
		}

		// 判断删除订单是否成功
		if (row > 0) {
			return true;
		}

		return false;
	}

	/**
	 * @Title: updateAllUserDetail @Description: 修改全部用户详情的Id(数据迁移专用) @author
	 *         yhq @return void @throws
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean updateAllUserDetail(List<Userdetail> userdetails) {
		// 判断返回用户账户列表是否为空
		if (userdetails != null && userdetails.size() > 0) {
			int number = 1;
			for (Userdetail userdetail : userdetails) {

				// 产生一个用户账户Id
				String oid = ToolUtil.getUid();

				userdetail.setUdId(oid);

				try {
					super.userDetailDao.save(userdetail);
					Thread.sleep(1);
				} catch (Exception e) {
					log.info("修改用户详情Id是出现异常   类：" + this.getClass() + " updateAllUserDetail()", e);
					return false;
				}
				number++;
			}
			return true;
		}

		return false;
	}

	public void firstAdd(String userId, Userdetail userdetail, DataResponse dataResponse, List<Userroute> list) {
		// 第一次将往返地址设置为路线
		Userroute userroute = new Userroute();
		Userroute userroute_return = new Userroute();

		try {
			list = this.setFirstRoute(userroute, userroute_return, list, userId, userdetail);
		} catch (ParseException e1) {
			log.info("日期转换异常或设置通勤路线异常", e1);
			return;
		}

		try {
			super.userRouteDao.save(userroute);
			super.userRouteDao.save(userroute_return);
		} catch (Exception e) {
			log.info("第一次存储用户路线失败,类" + this.getClass(), e);
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		dataResponse.setResponseBody(list);
		dataResponse.setResponseNumber("HH:mm");

	}

	/**
	 * 
	 * @throws ParseException
	 * @Title: setFirstRoute @Description:第一次根据地址设置路线 @param dataResponse
	 *         响应信息封装bean @param status 状态码 @author Li Yi @return void @throws
	 */
	public List<Userroute> setFirstRoute(Userroute userroute, Userroute userroute_return, List<Userroute> list,
			String userId, Userdetail userdetail) throws ParseException {
		// 路线Id
		userroute.setUrId(ToolUtil.getUid());
		// 用户Id
		userroute.setLoginId(userId);

		// SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		// String dateStr =
		// userdetail.getStartworkTime().toString().substring(11, 19);
		// ParsePosition pos = new ParsePosition(4);
		// Date datego = formatter.parse(dateStr,pos);
		// //时间(上班时间)
		// SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		// String dateStr = format.format(userdetail.getStartworkTime());
		// Date date = format.parse(dateStr);
		// userroute.setSetOutTime(date);
		userroute.setSetOutTime(userdetail.getStartworkTime());
		// userroute.setSetOutTime(userdetail.getStartworkTime());
		// 出发地址
		userroute.setStartAddress(userdetail.getHomeAddress());
		// 目的地址
		userroute.setPurposeAddress(userdetail.getCompanyAddress());
		// 出发经度
		userroute.setStartLongitude(userdetail.getHomeLongitude());
		// 出发纬度
		userroute.setStartLatitude(userdetail.getHomeLatitude());
		// 目的经度
		userroute.setPurposeLongitude(userdetail.getCompanyLongitude());
		// 目的纬度
		userroute.setPurposeLatitude(userdetail.getCompanyLatitude());
		// 路线类型(1: 上班 2:下班 3:附近)
		userroute.setRouteType(1);
		// 启用状态
		userroute.setIsEnable(1);

		// 路线ID
		userroute_return.setUrId(ToolUtil.getUid());
		// 用户ID
		userroute_return.setLoginId(userId);
		//// SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
		// String dateStr2 =
		//// userdetail.getStartworkTime().toString().substring(11, 19);
		// ParsePosition pos2 = new ParsePosition(4);
		// Date dateback = formatter2.parse(dateStr2,pos2);
		// 时间(下班时间)
		userroute_return.setSetOutTime(userdetail.getEndworkTime());
		// 出发地址
		userroute_return.setStartAddress(userdetail.getCompanyAddress());
		// 目的地址
		userroute_return.setPurposeAddress(userdetail.getHomeAddress());
		// 出发经度
		userroute_return.setStartLongitude(userdetail.getCompanyLongitude());
		// 出发纬度
		userroute_return.setStartLatitude(userdetail.getCompanyLatitude());
		// 目的经度
		userroute_return.setPurposeLongitude(userdetail.getHomeLongitude());
		// 目的纬度
		userroute_return.setPurposeLatitude(userdetail.getHomeLatitude());
		// 路线类型(1: 上班 2:下班 3:附近)
		userroute_return.setRouteType(2);
		// 启用状态
		userroute_return.setIsEnable(1);

		// 路线Userroute对象添加至list中
		list.add(userroute);
		list.add(userroute_return);

		return list;
	}

	/**
	 * @Title: getTokenByRongCloud
	 * @Description: 获取Token
	 * @param reqGetTokenByRongCloud
	 *            客户端获取Token请求数据
	 * @param dataResponse
	 *            响应数据
	 * @param @return
	 *            设定文件
	 * @author YHQ
	 * @return void 返回类型
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void getTokenByRongCloud(ReqGetTokenByRongCloud reqGetTokenByRongCloud, DataResponse dataResponse) {

		if (reqGetTokenByRongCloud == null || ToolUtil.isBlank(reqGetTokenByRongCloud.getUserId())) {
			log.error("获取Token时客户端传入数据为空");
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		// 根据用户Id获取用户详情
		Userdetail userdetail = super.userDetailDao.getUserDetailInfo(reqGetTokenByRongCloud.getUserId());

		// 判断用户详情是否为空
		if (userdetail == null || ToolUtil.isBlank(userdetail.getLoginId())
				|| ToolUtil.isBlank(userdetail.getNickName())) {
			setStatus(dataResponse, SystemCode.USER_INFO_NOT_EXIST_ERROR);
			return;
		}

		// 判断融云Token是否为空
		if (ToolUtil.isBlank(userdetail.getToken())) {
			// 获取token
			String token = instantMessenger.getToken(userdetail.getLoginId(), userdetail.getNickName(),
					ToolUtil.imageAddress(userdetail.getUserTabloidAvatar()), FormatType.json);

			// 判断如果token不为空时，给userDetail赋值
			if (token != null) {
				userdetail.setToken(token);
			}
		}

		dataResponse.setResponseBody(userdetail);

		setStatus(dataResponse, SystemCode.SUCCESS);// 请求成功

	}

	/**
	 * @Title: getUserDetailById
	 * @Description: 根据用户Id获取用户详情
	 * @param reqGetUserDetailById
	 *            请求参数Bean
	 * @param dataResponse
	 *            响应Bean
	 * @author YHQ
	 * @return void 返回类型
	 */
	@Override
	@Transactional(readOnly = true)
	public void getUserDetailById(ReqGetUserDetailById reqGetUserDetailById, DataResponse dataResponse) {

		// 判断请求参数是否为空
		if (reqGetUserDetailById == null || ToolUtil.isBlank(reqGetUserDetailById.getUserId())) {
			log.error("根据用户Id获取用户详情时，客户端请求参数为空");
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		// 创建用户详情对象
		Userdetail userdetail = null;
		// criterion查询，以用户id查询该用户的详细信息
		Criterion criterion = Restrictions.eq("loginId", reqGetUserDetailById.getUserId());

		try {

			userdetail = super.userDetailDao.queryCriteriaUnResult(criterion, false);
		} catch (Exception e) {

			log.error("查询用户详情出现异常, 类：" + this.getClass() + " 方法：getUserDetail()", e);
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		
		if(userdetail == null){
			
			Criterion special = Restrictions.eq("userId", reqGetUserDetailById.getUserId());
			
			Specialuserinfo specialuserinfo = null;
			try {
				specialuserinfo = criteriaBaseDao.queryCriteriaListUnique(Specialuserinfo.class, special);
			} catch (Exception e) {
				log.error("根据用户Id获取专车司机信息出现异常   类："+this.getClass(), e);
			}
			
			if(specialuserinfo != null){
				
				userdetail = new Userdetail();
				
				// 用户Id
				userdetail.setLoginId(specialuserinfo.getUserId());
				// 用户昵称
				userdetail.setNickName(specialuserinfo.getNickName());
				// 用户头像
				userdetail.setUserAvatar(specialuserinfo.getUserAvatar());
				// 用户头像缩略图
				userdetail.setUserTabloidAvatar(specialuserinfo.getUserTabloidAvatar());
				
			}
			
		}
		
		// 不存在该用户的详情
		if (userdetail == null) {
			log.error("不存在该用户的详情, 类：" + this.getClass() + " 方法：getUserDetail()");
			setStatus(dataResponse, SystemCode.USER_INFO_NOT_EXIST_ERROR);
			return;
		}

		userdetail.setUserAvatar(ToolUtil.imageAddress(userdetail.getUserAvatar()));
		userdetail.setUserTabloidAvatar(ToolUtil.imageAddress(userdetail.getUserTabloidAvatar()));

		dataResponse.setResponseBody(userdetail);
		
		setStatus(dataResponse, SystemCode.SUCCESS);
		
		log.info("获取用户昵称和头像响应数据: "+JSONUtil.toJSONString(dataResponse));
	}

	/**
	 * @Title: getUserBaseInfoById
	 * @Description: 获取用户基本信息
	 * @param reqGetUserDetailById
	 *            请求参数Bean
	 * @param dataResponse
	 *            响应Bean
	 * @author YHQ
	 * @return void 返回类型
	 */
	@Override
	@Transactional(readOnly = true)
	public void getUserBaseInfoById(ReqGetUserDetailById reqGetUserDetailById, DataResponse dataResponse) {

		// 判断请求参数是否为空
		if (reqGetUserDetailById == null || ToolUtil.isBlank(reqGetUserDetailById.getUserId())) {
			log.error("根据用户Id获取用户详情时，客户端请求参数为空");
			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		// 创建用户详情对象
		Userdetail userdetail = null;
		// criterion查询，以用户id查询该用户的详细信息
		Criterion criterion = Restrictions.eq("loginId", reqGetUserDetailById.getUserId());

		try {

			userdetail = super.userDetailDao.queryCriteriaUnResult(criterion, false);
		} catch (Exception e) {

			log.error("查询用户详情出现异常, 类：" + this.getClass() + " 方法：getUserDetail()", e);
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		// 不存在该用户的详情
		if (userdetail == null) {
			log.error("不存在该用户的详情, 类：" + this.getClass() + " 方法：getUserDetail()");
			setStatus(dataResponse, SystemCode.USER_INFO_NOT_EXIST_ERROR);
			return;
		}

		ResUserBaseInfo resUserBaseInfo = new ResUserBaseInfo();

		resUserBaseInfo.setLoginId(userdetail.getLoginId());
		resUserBaseInfo.setAge(userdetail.getAge());
		resUserBaseInfo.setNickName(userdetail.getNickName());
		resUserBaseInfo.setOccupation(userdetail.getOccupation());
		resUserBaseInfo.setOrigin(userdetail.getOrigin());
		resUserBaseInfo.setSex(userdetail.getSex());
		resUserBaseInfo.setUserLevel(userdetail.getUserLevel());
		dataResponse.setResponseBody(resUserBaseInfo);
		setStatus(dataResponse, SystemCode.SUCCESS);

	}

}
